import subprocess as tr
import random as r
import pygame as g

tr.call('clear', shell=True)

g.font.init()
g.mixer.init()

font = g.font.SysFont("ArcadeClassic", 16)

size = (600, 600)
s = 600

screen = g.display.set_mode(size)
g.display.set_caption('A Week With (the real) Man')

clock = g.time.Clock()

#klassy

class CamButton:
    def __init__(self, cam, pos):
        x, y = pos
        self.pos = pos
        self.o = g.Rect(x, y, 30, 30)
        self.num = cam
        self.txt = font.render(str(cam), True, (200, 200, 200))


class DoorButton:
    def __init__(self, pos, type, dor):
        x, y = pos
        self.o = g.Rect(x, y, 30, 30)
        self.door = dor
        self.type = type

class Door:
    def __init__(self, x, y):
        self.pos = (x, y)
        self.closed = False
        self.light = False

class dude:
    def __init__(self, acccam, starcam, door, AI, img, scimg):
        self.cams = acccam
        self.start = starcam
        self.AI = AI
        self.img = img
        self.cam = starcam
        self.turn = 0
        self.succ = False
        self.scare = False
        self.scimg = scimg
        self.door = door
    
    def TryMoove(self):
        self.succ = False
        bot = self.AI
        if cam == 'OFFICE' and bot <= 15 and doorz[self.door].closed:
            bot = 15
        if r.randint(bot, 20) == 20:
            if self.turn < len(self.cams):
                next = self.cams[self.turn]
                self.turn += 1
                self.cam = next
                self.succ = True
                print(self.cam)
            else:
                self.scare = True

#oderspraitz
class GenButton:
    def __init__(self, x, y):
        self.active = False
        self.o = g.Rect(x, y, 30, 30)
 
def blitgheng(target, source, location, opacity):
        x = location[0]
        y = location[1]
        temp = g.Surface((source.get_width(), source.get_height())).convert()
        temp.blit(target, (-x, -y))
        temp.blit(source, (0, 0))
        temp.set_alpha(opacity)
        target.blit(temp, location)

smoke = g.image.load('Assets/smoke.png')
fire = g.image.load('Assets/fire.png')

#setap  

title = True
run = True
game = True
mousebop = g.Rect(0, 0, 10, 10)
night = 1
WinWin = False

lises = [[0, 1, 2, 'OFFICE'], [0, 4, 2, 'OFFICE'], [0, 1, 4, 2, 'OFFICE'], [1, 3, 2, 'OFFICE'], [3, 5, 4, 2, 'OFFICE']]
lis2es = [[1, 4, 2, 5, 'OFFICE'], [4, 1, 5, 'OFFICE'], [1, 3, 5, 'OFFICE'], [0, 4, 3, 5, 'OFFICE'], [4, 1, 3, 5, 'OFFICE']]
fais = [1, 5, 8, 12, 15, 20]
fai2s = [3, 8, 10, 15, 17, 20]


#title
start = g.Rect(70, 330, 130, 30)

#back = g.image.load('back.png')
while game:
    while title:
        Closin = False
        mousebop.x, mousebop.y = g.mouse.get_pos()
        
        for ev in g.event.get():
            if ev.type == g.QUIT:
                run = False
                game = False
                title = False
                Closin = True 
                break
            if ev.type == g.MOUSEBUTTONDOWN:
                if ev.button == 1:
                    if start.colliderect(mousebop):
                        title = False
                        print('trep')
        
        back = g.image.load('Assets/backStart.png')
        
        screen.fill((0, 0, 0))
        
        screen.blit(back, (0, 0))
        
        scr = font.render(f'Continue form night {night}', True, (250, 250, 250))
        
        g.draw.rect(screen, (100, 100, 100), start)
        g.draw.rect(screen, (255, 100, 100), mousebop)
        screen.blit(scr, (start.x, start.y))
        
        g.display.flip()
        clock.tick(60)
    
    #stacco
    screen.fill((255, 0, 0))       
    g.display.flip()
    clock.tick(60)
    
    '''GAME'''
    
    '''SETUP GENTE'''
    gente = []
    muvtic = 50
    muvrate = 50
    
    lis = lises[night-1]
    lis2 = lis2es[night-1]
    Fai = fais[night-1]
    Bai = fai2s[night-1]
    
    gente.append(dude(lis, 0, 0, Fai, g.image.load('Assets/freddy.png'), g.image.load('Assets/freddyscare.png')))
    gente.append(dude(lis2, 1, 1, Bai, g.image.load('Assets/boi2.png'), g.image.load('Assets/boiscare.png')))
    
    deads = g.image.load('Assets/freddy.png')
    
    '''SETUP'''
    tic = 0
    
    precam = 0
    camposz = [(400, 300), (400, 350), (400, 400), (450, 350), (350, 300), (450, 400)]
    camere = []
    cam = 'OFFICE'
    
    ora = 0
    oraTick = 1000
    oraRate = 1000
    
    CamSwitch = g.Rect(100, 500, 400, 30)

    buttonz = [DoorButton((50, 311), 'door', 0), DoorButton((550, 300), 'door', 1), DoorButton((50, 351), 'lait', 0), DoorButton((550, 340), 'lait', 1)]
    doorz = [Door(98, 265), Door(460, 269)]
    
    win = False
    
    for i in range(len(camposz)):
        camere.append(CamButton(i, camposz[i]))
    
    pong = False
    
    #meccaniche treppanza
    
    fusione = 0
    fuscap = 2
    fusRate = 100
    fustic = 100
    fusioning = 15
    fused = False
    firin = 90
    
    fixer = GenButton(300, 100)
    
    #BATTERIA
    
    battery = 100
    usage = 1
    BatTic = 500
    BatRate = 500
    
    '''GAMELUP'''
    
    g.mixer.music.load('Assets/back.mp3')
    g.mixer.music.play(-1)
    
    while run:
        Closin = False
        mousebop.x, mousebop.y = g.mouse.get_pos()
        
        if tic >= oraTick:
            oraTick += oraRate
            ora += 1
            
            if ora == 6:
                win = True
                run = False
                game = True
        
        if tic >= BatTic:
            BatTic += BatRate
            battery -= usage
        
        if tic >= fustic:
            fustic += fusRate
            if usage > fuscap:
                fusione += usage-fuscap
                if fusione >= fusioning:
                    battery = 0
                    fused = True
                    g.mixer.music.load('Assets/alarm.mp3')
                    g.mixer.music.play(-1)
            
            if fixer.active:
                fusione -=1
                if fusione <= 0: fusione = 0
                
        
        Gbat = font.render(f'power left: {battery}%; Usage {usage}; GeneratorStress {fusione}/{fusioning}', True, (220, 220, 220))
        
        usage = 1
        
        for ev in g.event.get():
            if ev.type == g.QUIT:
                run = False
                game = False
                Closin = True
            
            if cam == 'OFFICE' and not pong:
                if mousebop.colliderect(CamSwitch):
                    cam = precam
                    pong = True
            else:  
                if mousebop.colliderect(CamSwitch) and not pong:
                    cam = 'OFFICE'
                    pong = True
            
            if not mousebop.colliderect(CamSwitch):
                pong = False
            
            if ev.type == g.MOUSEBUTTONDOWN and ev.button == 1:
                if cam != 'OFFICE':
                    for boi in camere:
                        if boi.o.colliderect(mousebop):
                            cam = boi.num
                            precam = cam
                else:
                    for boi in buttonz:
                        if boi.o.colliderect(mousebop):
                            if boi.type == 'door':
                                if doorz[boi.door].closed:
                                    doorz[boi.door].closed = False
                                else:
                                    doorz[boi.door].closed = True
                            else:
                                if doorz[boi.door].light:
                                    doorz[boi.door].light = False
                                else:
                                    doorz[boi.door].light = True
                    if fixer.o.colliderect(mousebop):
                        if fixer.active:
                            fixer.active = False
                        else:
                            fixer.active = True
            
            
        if battery <= 0:
            for dor in doorz:
                dor.closed = False
                dor.light = False
        
        for dor in doorz:
            if dor.closed:
                usage += 1
                dor.light = False
            if dor.light:
                usage += 1
        
        if fixer.active:
            usage += 1
        
        if cam != 'OFFICE':
            usage += 1
            for boi in doorz:
                boi.light = False
        
        if tic >= muvtic:
            muvtic += muvrate
            for boi in gente:
                boi.TryMoove()
                if boi.scare:
                    if not doorz[boi.door].closed:
                        deads = boi.scimg
                        run = False
                        break
                    else:
                        boi.turn = 0
                        boi.cam = boi.start
                        boi.scare = False
        
        screen.fill((0, 0, 0))
        
        bec = g.image.load(f'Assets/{cam}.png')
        screen.blit(bec, (0, 0))

        for boi in gente:
            if boi.cam == cam:
                if cam == 'OFFICE' and doorz[boi.door].closed:
                    1+1
                else:
                    if boi.door == 0 and (doorz[0].light or boi.cam != 'OFFICE'):
                        screen.blit(boi.img, (99, 266))
                    elif boi.door == 1 and (doorz[1].light or boi.cam != 'OFFICE'):
                        screen.blit(boi.img, (461, 270))

        if cam != 'OFFICE':
                for boi in camere:
                    g.draw.rect(screen, (50, 50, 50), boi.o)
                    screen.blit(boi.txt, boi.pos)   
                    MyBOi = font.render(f'cam {cam}', True, (200, 200, 200))
                    screen.blit(MyBOi, (50, 25))
        else:
            for boi in buttonz:
                g.draw.rect(screen, (100, 100, 100), boi.o)
            for boi in doorz:
                if boi.closed:
                    d = g.image.load(f'Assets/porta{doorz.index(boi)}.png')
                    screen.blit(d, boi.pos)
                elif boi.light:
                    d = g.image.load(f'Assets/luce{doorz.index(boi)}.png')
                    blitgheng(screen, d, boi.pos, 70)
            
            g.draw.rect(screen, (100, 255, 100), fixer.o)
    
    
        MyBOi = font.render(f'{ora} AM; Night {night}', True, (200, 200, 200))
        screen.blit(MyBOi, (450, 50))
        
        screen.blit(Gbat, (100, 50))
        
        g.draw.rect(screen, (100, 100, 100), CamSwitch)
        
        if fused:
            if firin <= 40:
                firin = 90
            else:
                firin -= r.randint(5, 10)
            blitgheng(screen, fire, (0, 0), firin)
            for i in range(r.randint(10, 40)):
                blitgheng(screen, smoke, (r.randint(0, 600), r.randint(0, 600)), 50)
        
        g.display.flip()
        clock.tick(60)
        tic += 1
       
    if win:
        night += 1
        if night == 7:
            WinWin = True
            game = False
            break
        else:
            g.mixer.music.load('Assets/end_night.mp3')
            g.mixer.music.play()
    elif not Closin:
        g.mixer.music.load('Assets/scream.mp3')
        g.mixer.music.play()
        
        for i in range(100):
            for ev in g.event.get():
                if ev.type == g.QUIT:
                    run = False
                    title = False
                    game = False
                    break
            
            screen.blit(deads, (0, 0))
            g.display.flip()
            clock.tick(60)
    
    else:
        print('bye')
    
    title = True
    run = True
        


if WinWin:
    g.mixer.music.load('Assets/win.mp3')
    g.mixer.music.play()
    
    while True:
        for ev in g.event.get():
            if ev.type == g.QUIT:
                break
            
        screen.fill((150, 150, 25))
        img = g.image.load('Assets/GoodEnd.png')
        screen.blit(img, (0, 0))
            
        g.display.flip()
        clock.tick(60)

try:
    g.quit()
except:
    print('')
print('Thanks 4 playin\'!')

